
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <utility>
#include <time.h>
#include <string>
#include <EasyBMP.h>

#define BMP_DIR "C:\\Users\\Frank Erdesz\\Documents\\DropletCode\\cu-droplet\\StudentCode\\FrankErdesz\\Test_Bitmaps\\"
#define DEBUG false

void createRandomVector(BMP &imgData, int n, std::vector<std::pair<int, int>> *_out_xy_values)
{
	int height = imgData.TellHeight();
	int width = imgData.TellWidth();

	//this part generates the vector of xy pairs
	//pair<int, int> xy_pair;
	//vector<pair<int,int>> xy_Vals; 
	//int i = 0;
	
	for (int i = 0; i < n; i++) {
		
		//xy_pair.first = rand () % width;
		//xy_pair.second = rand () % height;

		//xy_Vals.push_back(xy_pair);
		_out_xy_values->push_back(std::make_pair
			(
				(int)(((float)rand() / (float)RAND_MAX) * (width - 1)), 
				(int)(((float)rand() / (float)RAND_MAX) * (height - 1))
			)
		); 
	}
}

void getPixelData (
	BMP &imgData,
	int n, 
	std::vector<std::pair<int, int>> *xy_values, 
	std::vector<unsigned char *> *_out_rgb_values)
{

	std::vector<std::pair<int, int>>::iterator xy_it;
	std::vector<unsigned char *>::iterator rgb_it;

	xy_it = xy_values->begin();
	rgb_it = _out_rgb_values->begin();

	int x, y;

	for(int i = 0; i < n; i++){
		  
		x = xy_it->first;
		y = xy_it->second;

		unsigned char *rgbVal = *rgb_it;

		rgbVal[0] = imgData(x, y)->Red;
		rgbVal[1] = imgData(x, y)->Green;
		rgbVal[2] = imgData(x, y)->Blue;

		xy_it++;
		rgb_it++;
	}
}


void main(int argc, char *argv[])
{
	bool debug = DEBUG;

	std::string bmpFilepath(BMP_DIR);
	int numDropletsStart = atoi(argv[1]);
	int numDropletsStop = atoi(argv[2]);
	int numDropletsInc = atoi(argv[3]);
	bmpFilepath.append(argv[4]);

	BMP imgData;
	if(!imgData.ReadFromFile(bmpFilepath.c_str()))
	{
		std::cerr << "Could not open BMP file : " << bmpFilepath << std::endl;
		return;
	}
	else
	{
		std::cout << "Opened file : " << bmpFilepath <<
			std::endl << " Image size " << imgData.TellWidth() << " x " << imgData.TellHeight()
			<< std::endl;
	}
	
	clock_t t1, t2; //timing part 1/2

	for(int n = numDropletsStart; n <= numDropletsStop; n += numDropletsInc)
	{
		std::vector<std::pair<int, int>> xyVals;
		std::vector<unsigned char *> rgbVals;

		xyVals.clear();
		rgbVals.clear();

		createRandomVector(imgData, n, &xyVals);

		for(int i = 0; i < n; i++)
		{
			unsigned char *rgbVal = (unsigned char *)malloc(sizeof(unsigned char) * 3);
			rgbVals.push_back(rgbVal);
		}

		t1 = clock ();
		getPixelData(imgData, n, &xyVals, &rgbVals);
		t2 = clock(); //timing part 2/2

		double diff ((double)t2 - (double)t1);
		std::cout << "Timing Information\t" << n << " Droplets :\t"  
			<< diff * ((double)1000 / CLOCKS_PER_SEC) << " ms" << std::endl;

		std::vector<unsigned char *>::reverse_iterator rit;
		for(rit = rgbVals.rbegin(); rit != rgbVals.rend(); rit++)
		{
			if(debug)
			{
				unsigned char *rgbVal = *rit;
				std::cout << "(" << (int)rgbVal[0] << ", " 
					<< (int)rgbVal[1] << ", " << (int)rgbVal[2] << ") ";
			}
			free(*rit);
		}
		if(debug)
		{
			std::cout << std::endl << "=========================================" 
				<< std::endl;
		}
	}
}